package com.springboot.blog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.blog.dto.LayResponse;
import com.springboot.blog.entity.Menu;
import com.springboot.blog.interfaces.ToolInterface;
import com.springboot.blog.service.IMenuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
class AdminMenuController {
    @Autowired
    IMenuService menuService;
    String PATH = "admin/menu/";

    @RequiresPermissions("menu:list")
    @GetMapping(value = {"/admin/menu/index", "/admin/menu"})
    public String index() {
        return PATH + "index";
    }

    @RequiresPermissions("menu:list")
    @PostMapping(value = {"/admin/menu/tree"})
    @ResponseBody
    public LayResponse tree() {
        QueryWrapper<Menu> wrapper = new QueryWrapper<>();
        List<Menu> categories = menuService.list(wrapper.lambda().orderByAsc(Menu::getSorts));
        return new LayResponse().success("成功!").data(categories).setCount(9L);
    }

    @RequiresPermissions("menu:delete")
    @PostMapping(value = {"/admin/menu/delete"})
    @ResponseBody
    public LayResponse delete(@RequestParam int id) {
        LayResponse layResponse = new LayResponse();
        Menu one = menuService.getOne(new LambdaQueryWrapper<Menu>().eq(Menu::getPid, id).select(Menu::getId).last("limit 1"));
        if(null!=one){
            return layResponse.error("清先删除所有子节点后再进行删除!");
        }
        boolean result = menuService.removeById(id);
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("menu:edit")
    @PostMapping(value = {"/admin/menu/ajax"})
    @ResponseBody
    public LayResponse ajax(Menu menu) {
        boolean b = menuService.updateById(menu);
        LayResponse layResponse = new LayResponse();
        if (b) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("menu:add")
    @GetMapping(value = {"/admin/menu/add"})
    public String add(Model model) {
        List<Menu> list = menuService.list(new QueryWrapper<Menu>().lambda().orderByAsc(Menu::getSorts));
        List<Menu> list_sort = new ArrayList<>();
        unlimitedSort(list, list_sort, 0, 0);
        model.addAttribute("list", list_sort);
        return PATH + "add";
    }

    @RequiresPermissions("menu:add")
    @PostMapping(value = {"/admin/menu/add"})
    @ResponseBody
    public LayResponse add(@Validated(ToolInterface.insert.class) Menu menu) {
        menuService.save(menu);
        return new LayResponse().success();
    }

    private void unlimitedSort(List<Menu> list, List<Menu> list_sort, int pid, int level) {
        if(level<2){
            for (Menu item : list) {
                if (item.getPid() == pid) {
                    item.setTitle(StringUtils.repeat("　　", level) + "├" + item.getTitle());
                    list_sort.add(item);
                    unlimitedSort(list, list_sort, item.getId(), level + 1);
                }
            }
        }
    }

    @RequiresPermissions("menu:edit")
    @GetMapping(value = {"/admin/menu/edit"})
    public String edit(Model model, @RequestParam Integer id) {
        List<Menu> list = menuService.list(new QueryWrapper<Menu>().lambda().orderByAsc(Menu::getSorts));
        List<Menu> list_sort = new ArrayList<>();
        unlimitedSort(list, list_sort, 0, 0);
        Menu menu = menuService.getById(id);

        model.addAttribute("list", list_sort);
        model.addAttribute("data", menu);
        return PATH + "edit";
    }

    @RequiresPermissions("menu:add")
    @PostMapping(value = {"/admin/menu/edit"})
    @ResponseBody
    public LayResponse edit(@Validated(ToolInterface.update.class) Menu menu) {
        menuService.updateById(menu);
        return new LayResponse().success();
    }
}
